package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.FlowElementProcessContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CustomPropertiesProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowTitleExpression;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowCustomTitleRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTitleExpressionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: lzb
 * @date: 2021/11/9 17:04
 */
@Service
public class CustomTitlePropertiesProcessor implements CustomPropertiesProcessor {

    @Autowired
    private FlowTitleExpressionRepository flowTitleExpressionRepository;

    @Autowired
    private FlowCustomTitleRepository flowCustomTitleRepository;

    public static Pattern dynamic = Pattern.compile(".*\\@\\{([A-Za-z]+)\\}.*");
    public static Pattern dynamicLimitCount = Pattern.compile("\\@\\{([A-Za-z]+)\\}");


    @Override
    public boolean support(JsonNode elementNode) {
        JsonNode properties = elementNode.get("properties");
        if (nonNull(properties)) {
            JsonNode customTitleContent = properties.get("customTitle");
            return nonNull(customTitleContent);
        }
        return false;
    }

    @Override
    public void process(FlowElementProcessContext context) {
        JsonNode customTitleConfigNode = context.getElementNode().get("properties");
        if (Objects.nonNull(customTitleConfigNode)) {
            JsonNode customTitleConfigNodeObj = customTitleConfigNode.get("customTitle");
            if (Objects.nonNull(customTitleConfigNodeObj)) {
                List<String> nodeCustomTitleExpression = new ArrayList<>();
                for (int i = 0; i < customTitleConfigNodeObj.size(); i++) {
                    JsonNode customTitleInfo = customTitleConfigNodeObj.get(i);
                    String titleExpression = customTitleInfo.get("titleExpression").textValue();
                    nodeCustomTitleExpression.add(titleExpression);
                }
                handleCustomTitleExpression(nodeCustomTitleExpression, context);
            }
        }
    }

    public void handleCustomTitleExpression(List<String> expressionList, FlowElementProcessContext context) {
        if (!CollectionUtils.isEmpty(expressionList)) {
//            String expression = String.join(",", isTitleExpression(expressionList));
//            List<String> customTitleExpressionContent = isContainsDynamicParameter(expression);
//            if (!CollectionUtils.isEmpty(customTitleExpressionContent)) {
                String titleExpression = String.join(",", expressionList);
                FlowTitleExpression flowTitleExpression = new FlowTitleExpression();
                flowTitleExpression.setDeployId(context.getDeployId());
                flowTitleExpression.setExpression(titleExpression);
                flowTitleExpression.setActivityId(context.getActivity().getId());
                flowTitleExpressionRepository.insertFlowTitleExpression(flowTitleExpression);
//            }
            //批量添加
//            List<FlowTitleExpression> flowTitleExpressionList = new ArrayList<>();
//            if (!CollectionUtils.isEmpty(customTitleExpressionContent)) {
//                for (String expre : customTitleExpressionContent) {
//                    FlowTitleExpression flowTitleExpression = new FlowTitleExpression();
//                    flowTitleExpression.setDeployId(context.getDeployId());
//                    flowTitleExpression.setExpression(expre);
//                    flowTitleExpression.setActivityId(context.getActivity().getId());
//                    flowTitleExpressionList.add(flowTitleExpression);
//                }
//            }
//            flowTitleExpressionRepository.batchInsert(flowTitleExpressionList);
        }
    }

    public List<String> isTitleExpression(List<String> expressionList) {
        Iterator<String> iterator = expressionList.iterator();
        List<String> customTitleExpressionList = flowCustomTitleRepository.getCustomTitleExpression();
        while (iterator.hasNext()) {
            if (!customTitleExpressionList.contains(iterator.next())) {
                iterator.remove();//使用迭代器的删除方法删除
            }
        }
        return expressionList;
    }

    /**
     * 正则校验 @{} 格式
     *
     * @param content
     * @return
     */
    public static List<String> isContainsDynamicParameter(String content) {
        Set paramSet = new LinkedHashSet<>();
        if (dynamic.matcher(content).matches()) {
            Matcher m = dynamicLimitCount.matcher(content);
            while (m.find()) {
                paramSet.add(m.group(1));
            }
        }
        return new ArrayList<>(paramSet);
    }


}
